<?php

namespace common\models;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;

/**
 * This is the model class for table "mdm_task".
 *
 * @property int $id
 * @property string $input
 * @property int $flag
 * @property string $output
 * @property string $started_at
 * @property string $stopped_at
 * @property string $created_by
 * @property string $created_at
 * @property string $updated_by
 * @property string $updated_at
 * @property int $workID
 */
class MdmTask extends \yii\db\ActiveRecord
{
    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('db_mdm_etl');
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mdm_task';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['flag', 'workID'], 'integer'],
            [['started_at', 'stopped_at', 'created_at', 'updated_at'], 'safe'],
            [['input', 'output'], 'string', 'max' => 4096],
            [['created_by', 'updated_by'], 'string', 'max' => 45],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'input' => Yii::t('app', 'Input'),
            'flag' => Yii::t('app', 'Flag'),
            'output' => Yii::t('app', 'Output'),
            'started_at' => Yii::t('app', 'Started At'),
            'stopped_at' => Yii::t('app', 'Stopped At'),
            'created_by' => Yii::t('app', 'Created By'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_by' => Yii::t('app', 'Updated By'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'workID' => Yii::t('app', 'Work ID'),
        ];
    }

    public function behaviors()
    {
        return [
            [
                /**
                 * TimestampBehavior：
                 * 创建的时候，默认插入当前时间戳给created_at和updated_at字段
                 * 更新的时候，默认更新当前时间戳给updated_at字段
                 */
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at', 'started_at',],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at', 'stopped_at'],
                ],
//                'value' => new Expression('NOW()'),
//              改为MSSQL Server
                'value' => new Expression('GETDATE()'),
            ],
//            [
//                /**
//                 * BlameableBehavior：
//                 * 创建的时候，默认插入当前用户ID给author_id和updater_id字段
//                 * 更新的时候，默认更新当前用户ID给updater_id字段
//                 */
//                'class' => BlameableBehavior::className(),
//                'createdByAttribute' => 'created_by',
//                'updatedByAttribute' => 'updated_by',
//                'value'              => Yii::$app->user->identity->username,
//            ],
        ];
    }
}
